#include <IRremote.h>
IRsend irsend;
#define NEC_HDR_MARK    9000
#define NEC_HDR_SPACE   4500
#define NEC_BIT_MARK     650
#define NEC_ONE_SPACE   1750
#define NEC_ZERO_SPACE   600

#define BEEP 8
#define CHECK 10
#define OPEN A3
#define ADD A1
#define MIN 12
#define CASH 3
boolean CASHON = false;

int temperature = 26;
int temp[][4] = { 
  {0,0,0,0},  // 16
  {1,0,0,0},  // 17
  {0,1,0,0},  // 18
  {1,1,0,0},  // 19
  {0,0,1,0},  // 20
  {1,0,1,0},  // 21
  {0,1,1,0},  // 22
  {1,1,1,0},  // 23
  {0,0,0,1},  // 24
  {1,0,0,1},  // 25
  {0,1,0,1},  // 26
  {1,1,0,1},  // 27
  {0,0,1,1},  // 28
  {1,0,1,1},  // 29
  {0,1,1,1},  // 30
};

int sum_on[][4] = { 
  {1,0,1,0},  // 16
  {0,1,1,0},  // 17
  {1,1,1,1},  // 18
  {0,0,0,1},  // 19
  {1,0,0,1},  // 20
  {0,1,0,1},  // 21
  {1,1,0,1},  // 22
  {0,0,1,1},  // 23
  {1,0,1,1},  // 24
  {0,1,1,1},  // 25
  {1,1,1,1},  // 26
  {0,0,0,0},  // 27
  {1,0,0,0},  // 28
  {0,1,0,0},  // 29
  {1,1,0,0},  // 30
  {0,0,1,0},  // +1
  {1,0,1,0},  // +2
  {0,1,1,0},  // +3
};

int sum_off[][4] = { 
  {0,1,1,1},  // 16
  {1,1,1,1},  // 17
  {0,0,0,0},  // 18
  {1,0,0,0},  // 19
  {0,1,0,0},  // 20
  {1,1,0,0},  // 21
  {0,0,1,0},  // 22
  {1,0,1,0},  // 23
  {0,1,1,0},  // 24
  {1,1,1,0},  // 25
  {0,0,0,1},  // 26
  {1,0,0,1},  // 27
  {0,1,0,1},  // 28
  {1,1,0,1},  // 29
  {0,0,1,1},  // 30
};

// default cooling(1)
int mode[][3] = {
  {0,0,0},
  {1,0,0},
  {0,1,0},
  {1,1,0}
//  {0,0,1}
};
int set_mode = 1;

enum STATE
{
  on,off
} 
state;

void setup() {
  pinMode(BEEP, OUTPUT);
  digitalWrite(BEEP,LOW);
  pinMode(CASH, OUTPUT);
  digitalWrite(CASH,LOW);

  pinMode(OPEN, INPUT);
  pinMode(ADD, INPUT);
  pinMode(MIN, INPUT);
  pinMode(CHECK,INPUT);

  beep();
  state = off;
}

void loop() {
  if (digitalRead(OPEN))
  {
    delay(50);
    if (digitalRead(OPEN))
    {
      while(digitalRead(OPEN))
      {
        if(digitalRead(CHECK))
        {
          delay(50);
          while(digitalRead(CHECK));
          beep();
          cash();
        }
      };
      beep();
      if(state==off)
      {
        greeOn();
        state = on;
      }
      else
      {
        greeOff();
        state = off;
      }
    }
  }
  
  if (digitalRead(ADD))
  {
    delay(50);
    if (digitalRead(ADD))
    {
      while(digitalRead(ADD));
      beep();
      temperature++;
      if(temperature>30)
        temperature = 30;
        greeOn();
        state = on;
    }
  }
  
  if (digitalRead(MIN))
  {
    delay(50);
    if (digitalRead(MIN))
    {
      while(digitalRead(MIN));
      beep();
      temperature--;
      if(temperature<16)
        temperature = 16;
        greeOn();
        state = on;
    }
  }
  
  if (digitalRead(CHECK))
  {
    delay(50);
    if (digitalRead(CHECK))
    {
      while(digitalRead(CHECK));
      set_mode++;
      if(set_mode>3)
        set_mode = 1;
      greeOn();
      state = on;
      beep();
      //cash();
    }
  }
}

void beep()
{
  digitalWrite(BEEP, HIGH);
  delay(100);
  digitalWrite(BEEP, LOW);
}

void cash()
{
  CASHON = ~CASHON;
  digitalWrite(CASH, CASHON);
}

void sendFour(int one,int two,int three,int four)
{
  if(one)
  {
    irsend.mark(NEC_BIT_MARK);
    irsend.space(NEC_ONE_SPACE);
  }
  else
  {
    irsend.mark(NEC_BIT_MARK);
    irsend.space(NEC_ZERO_SPACE);
  };
  if(two)
  {
    irsend.mark(NEC_BIT_MARK);
    irsend.space(NEC_ONE_SPACE);
  }
  else
  {
    irsend.mark(NEC_BIT_MARK);
    irsend.space(NEC_ZERO_SPACE);
  };
  if(three)
  {
    irsend.mark(NEC_BIT_MARK);
    irsend.space(NEC_ONE_SPACE);
  }
  else
  {
    irsend.mark(NEC_BIT_MARK);
    irsend.space(NEC_ZERO_SPACE);
  };
  if(four)
  {
    irsend.mark(NEC_BIT_MARK);
    irsend.space(NEC_ONE_SPACE);
  }
  else
  {
    irsend.mark(NEC_BIT_MARK);
    irsend.space(NEC_ZERO_SPACE);
  };
}

void greeWetOn()
{
  irsend.enableIROut(38);

  // Header
  irsend.mark(NEC_HDR_MARK);
  irsend.space(NEC_HDR_SPACE);

  sendFour(0,1,0,1);
  sendFour(0,0,0,0);
  sendFour(0,1,0,1);
  sendFour(0,0,0,0);
  sendFour(0,0,0,0);
  sendFour(0,1,0,0);
  sendFour(0,0,0,0);
  sendFour(1,0,1,0);

  //-------010
  // 0
  irsend.mark(NEC_BIT_MARK);
  irsend.space(NEC_ZERO_SPACE);
  // 1
  irsend.mark(NEC_BIT_MARK);
  irsend.space(NEC_ONE_SPACE);
  // 0
  irsend.mark(NEC_BIT_MARK);
  irsend.space(NEC_ZERO_SPACE);

  irsend.mark(NEC_BIT_MARK);
  irsend.space(20000);

  sendFour(0,0,1,0);
  sendFour(1,0,0,0);
  sendFour(0,0,0,0);
  sendFour(0,1,0,0);
  sendFour(0,0,0,0);
  sendFour(0,0,0,0);
  sendFour(0,0,0,0);
  sendFour(1,0,0,0);

  // Footer
  irsend.mark(NEC_BIT_MARK);
  irsend.space(0);  // Always end with the LED off
}

void greeOn()
{
  irsend.enableIROut(38);

  // Header
  irsend.mark(NEC_HDR_MARK);
  irsend.space(NEC_HDR_SPACE);

  sendFour(mode[set_mode][0],mode[set_mode][1],mode[set_mode][2],1);  // 1-3: mode 4:switch
  sendFour(1,0,0,0);
  sendFour(temp[temperature-16][0],temp[temperature-16][1],temp[temperature-16][2],temp[temperature-16][3]);  
  sendFour(0,0,0,0);
  sendFour(0,0,0,0);
  sendFour(0,1,0,0);
  sendFour(0,0,0,0);
  sendFour(1,0,1,0);

  //-------010
  // 0
  irsend.mark(NEC_BIT_MARK);
  irsend.space(NEC_ZERO_SPACE);
  // 1
  irsend.mark(NEC_BIT_MARK);
  irsend.space(NEC_ONE_SPACE);
  // 0
  irsend.mark(NEC_BIT_MARK);
  irsend.space(NEC_ZERO_SPACE);

  irsend.mark(NEC_BIT_MARK);
  irsend.space(20000);

  sendFour(0,0,1,0);
  sendFour(1,0,0,0);
  sendFour(0,0,0,0);
  sendFour(0,1,0,0);
  sendFour(0,0,0,0);
  sendFour(0,0,0,0);
  sendFour(0,0,0,0);
  
  sendFour(sum_on[temperature-16+set_mode][0],sum_on[temperature-16+set_mode][1],sum_on[temperature-16+set_mode][2],sum_on[temperature-16+set_mode][3]);

  // Footer
  irsend.mark(NEC_BIT_MARK);
  irsend.space(0);  // Always end with the LED off
}

void greeOff()
{
  irsend.enableIROut(38);

  // Header
  irsend.mark(NEC_HDR_MARK);
  irsend.space(NEC_HDR_SPACE);

  sendFour(1,0,0,0);
  sendFour(1,0,0,0);
  sendFour(temp[temperature-16][0],temp[temperature-16][1],temp[temperature-16][2],temp[temperature-16][3]);
  sendFour(0,0,0,0);
  sendFour(0,0,0,0);
  sendFour(0,1,0,0);
  sendFour(0,0,0,0);
  sendFour(1,0,1,0);

  //-------010
  // 0
  irsend.mark(NEC_BIT_MARK);
  irsend.space(NEC_ZERO_SPACE);
  // 1
  irsend.mark(NEC_BIT_MARK);
  irsend.space(NEC_ONE_SPACE);
  // 0
  irsend.mark(NEC_BIT_MARK);
  irsend.space(NEC_ZERO_SPACE);

  irsend.mark(NEC_BIT_MARK);
  irsend.space(20000);

  sendFour(0,0,1,0);
  sendFour(1,0,0,0);
  sendFour(0,0,0,0);
  sendFour(0,1,0,0);
  sendFour(0,0,0,0);
  sendFour(0,0,0,0);
  sendFour(0,0,0,0);
  sendFour(sum_off[temperature-16][0],sum_off[temperature-16][1],sum_off[temperature-16][2],sum_off[temperature-16][3]);

  // Footer
  irsend.mark(NEC_BIT_MARK);
  irsend.space(0);  // Always end with the LED off

}





